<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/vectrics.tld"    prefix="vectrics" %>
<%@ taglib uri="/WEB-INF/tld/hexacta.tld"  prefix="hexacta" %>

<%@page import="org.vectrics.recruiter.struts.employee.EmployeeForm" %>
<%@page import="org.vectrics.recruiter.job.JobTerm"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.vectrics.recruiter.RecruitServiceLocator"%>
<%@page import="org.vectrics.hibernate.ThreadProperties"%>

<script type="text/javascript" src="/hrs-intranet/js/utils.js" > </script>
<link rel="stylesheet" href="/hrs-intranet/styles/displaytag.css" type="text/css" />
<jsp:useBean id="employeeForm" class="org.vectrics.recruiter.struts.employee.EmployeeForm" scope="request"/>

<%
  String functionCode = "";
  if (request.getAttribute("function") != null) {
     functionCode = request.getAttribute("function").toString();  
  }
  String medicalInsuranceCode = "";
  if (request.getAttribute("medicalInsurance") != null) {
	 medicalInsuranceCode  = request.getAttribute("medicalInsurance").toString();  
  }
  String locationCode = "";
  if (request.getAttribute("location") != null) {
	 locationCode = request.getAttribute("location").toString();  
  }
  
	// Busco de la request el id de la persona y el id de la compania
  String idPerson = request.getParameter("personId"); 
  String idCompany = request.getParameter("companyId");
  
  String actionMethod = (request.getParameter("actionMethod")!=null) ? request.getParameter("actionMethod") : "";
  
  if (idPerson==null) {
	  idPerson = (String) request.getAttribute("personId"); 
  }
  if (idCompany==null) {
	  idCompany = (String) request.getAttribute("companyId"); 
  }
  List<JobTerm> status = new ArrayList<JobTerm>();

  if ((idPerson!=null) && (idCompany!=null)) {
  	  status.addAll(RecruitServiceLocator.getCompanyService()
  			  .findStatusForPersonId(new Long(idPerson), new Long(idCompany)));
  }

  request.setAttribute("statusList", status);
  String userName = ThreadProperties.getPerson().getUser().getUserName();
  String password = ThreadProperties.getPerson().getUser().getPassword();
  String domainId = ThreadProperties.getDomain().getId().toString();
  String contextPath = request.getContextPath();
  
  String viewOption = ("ADD".equals(actionMethod)) ? "false" : "true";
  String phraseButton = ("ADD".equals(actionMethod)) ? "button.add" : "button.ok";
%>

<script type="text/javascript">

     
	function reviewCondition() {
		var id = document.getElementById("id").value; 
		var select = document.getElementById("status"); 
		var href = "<%=contextPath%>/ws?command=get.employee.condition&user=<%=userName%>&domainId=<%=domainId%>&pcode=<%=password%>";
		    href += "&state="+select.value;
		    href += "&id="+ id;
		remoteCall(href, handleXmlSearchResponse); 
    }

    function changeCondition(select) {
		var id = document.getElementById("id").value; 
		var href = "<%=contextPath%>/ws?command=get.employee.condition&user=<%=userName%>&domainId=<%=domainId%>&pcode=<%=password%>";
		    href += "&state="+select.value;
		    href += "&id="+ id;
		remoteCall(href, handleXmlSearchResponse); 
	}

 	function handleXmlSearchResponse() {
		  // if the readyState code is 4 (Completed)
		  // and http status is 200 (OK) we go ahead and get the responseText
		  // other readyState codes:
		  // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
		  var divfecha = document.getElementById('divfecha');
		  var fechatxt = document.getElementById('fechatxt');
		  var dateTR=document.getElementById('dateTR');
		  
		  var divLastAgreement = document.getElementById('divLastAgreement');
		  
		  log("Response received from server, processing XML response.");
		  var xmlhttp = getXmlHttp();
		  if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
						var xmlDoc = AjxXmlDoc.createFromXml(xmlhttp.responseText);
						var nativeDocument = xmlDoc.getDoc();
						var reference  = nativeDocument.getElementsByTagName("reference")[0];
					    var isRequiredDate = reference.getAttribute("isRequieredDate");
						if (isRequiredDate=="true") {
							if (fechatxt.childNodes[0]!=null) {
									fechatxt.removeChild(fechatxt.childNodes[0]);
							}
							var texto = document.createTextNode(reference.getAttribute("phase")+" *");
							fechatxt.appendChild(texto);	
							dateTR.style.display = "";
					    	divfecha.style.display = "";

							if( reference.getAttribute("phase") != "Last working day" ) {
								divLastAgreement.style.display = "";
							} else {
								divLastAgreement.style.display = "none";
							}

					    	var isEquals = reference.getAttribute("statusEquals");
					    	var date = document.getElementById('date');
					    	if (isEquals=="true") {
								date.value = reference.getAttribute("date");
						    } else {
						    	date.value = "";
							}
						} else {
							dateTR.style.display = "none";
							divfecha.style.display = "none";
							divLastAgreement.style.display = "none";
						}
				} else {
			  		log("Not processed - Response status = " + xmlhttp.status);
				}
		  } else {
		  		log("Not processed - Response readyState = " + xmlhttp.readyState);
		  }
    }


</script>
<% if (!"ADD".equals(actionMethod)) { %>
	<table align="right" >
    	<tr>
			<td valign="top" align="right">
				<table cellpadding="0" cellspacing="0" width="100%">
					<tr>
						<td>
							<form action="/hrs-intranet/admin/employee/condition/add.do" method="POST">
								<input type="hidden" name="personId" value="<%=idPerson%>" />
								<input type="hidden" name="companyId" value="<%=idCompany%>" />
								<input type="hidden" name="actionMethod" value="ADD" />
								
								<vectrics:formButton code="button.param" phraseCode="button.employee.condition.add" value="submit" 
									elementId="employee.condition.add"
									name="submit"/>
							</form>							
						</td>
					</tr>
				</table>
      		</td>
     	</tr>
   </table>
<% } %>
<vectrics:errors/>

<html:form action="/admin/employee/save" method="POST">
	<html:hidden property="id"  styleId="id"  />
    <html:hidden  property="actionMethod" value="<%=actionMethod%>" />

    <table>
		<tr>
			<td width="50%">
				<table cellpadding="4" cellspacing="1">
					<tr>
						<td class="form1bak" valign="middle" style="width:128px;">
							<font class="subFont">
								<bean:message key="prompt.employee.record.effective.day"/> *
							</font>
						<vectrics:errors property="recordEffectiveDay" />
						</td>
						<td class="form1bak" valign="top" colspan="2">
							<html:text property="recordEffectiveDay" styleClass="formButton" styleId="recordEffectiveDay" maxlength="10"/>
							<input type="button" id="recordEffectiveDayBt" name="recordEffectiveDayBt" value="..." /> 
										<script type="text/javascript">
										Calendar.setup(
												{
  												inputField  : "recordEffectiveDay",         // ID of the input field
  												ifFormat    : "%d/%m/%Y",    // the date format
  												button      : "recordEffectiveDayBt"       // ID of the button
												}
										);
										</script>
						</td>
					</tr>
					<tr>
						<td class="form1bak" valign="middle" style="width:128px;">
							<font class="subFont">
								<bean:message key="prompt.employee.date.start"/> *
							</font>
						<vectrics:errors property="dateStart" />
						</td>
						<td class="form1bak" valign="top" colspan="2">
							<html:text property="dateStart" styleClass="formButton" styleId="dateStart" maxlength="10"/>
							<input type="button" id="dateStartBt" name="dateStartBt" value="..." /> 
										<script type="text/javascript">
										Calendar.setup(
												{
  												inputField  : "dateStart",         // ID of the input field
  												ifFormat    : "%d/%m/%Y",    // the date format
  												button      : "dateStartBt"       // ID of the button
												}
										);
										</script>
						</td>
					</tr>
					<tr>
						<td class="form1bak" valign="middle">
							<font class="subFont">
								<bean:message key="prompt.employee.condition"/> *
							</font>
						<vectrics:errors property="condition" />
						</td>
						<td class="form1bak" valign="middle" >
								<html:select property="status" styleId="status" styleClass="formComboClass" onchange=" changeCondition(this); " >
								    <html:options 
								        collection="statusList" 
										property="code"
								        labelProperty="description"
									 />
								</html:select>
                        </td>
                    </tr>
					<tr id="dateTR">
                        <td class="form1bak" valign="middle" >
                                 <div id="fechatxt" class="subFont">
						         </div>
						         <vectrics:errors property="date" />
						</td>
						<td class="form1bak" valign="top">
							     <html:text property="date" styleId="date" styleClass="formButton" maxlength="10"/>
							     <input type="button" id="dateBt" name="dateBt" value="..." /> 
										<script type="text/javascript">
  												Calendar.setup(
    												{
      												inputField  : "date",         // ID of the input field
      												ifFormat    : "%d/%m/%Y",    // the date format
      												button      : "dateBt"       // ID of the button
    												}
  												);
										</script>
					
						 </td>
                    </tr>
					<tr>
						<td class="form1bak" valign="middle">
							<font class="subFont">
								<bean:message key="prompt.employee.function"/> <b>*</b>
							</font>
						</td>
						<td class="form1bak" valign="top" colspan="2">
							<vectrics:jobFunctionCombo property="function" value="<%=functionCode %>" />
						</td>
					</tr>

					<tr> 
						<td class="form1bak" valign="middle">
							<font class="formLabelClass"><bean:message key="prompt.employee.career.rolePosition"/> <b>*</b> </font>
						</td>
						<td class="form1bak" valign="top" colspan="2" >
							<html:text styleClass="formButton" maxlength="3" property="careerRolePosition" onkeypress="return soloNumerosConNegativos(event,this);" /><font class="formLabelClass">&nbsp; %</font>
           				</td>
					</tr>

					<tr> 
						<td class="form1bak" valign="middle">
							<font class="formLabelClass"><bean:message key="prompt.employee.hoursPerDay"/> <b>*</b> </font>
						</td>
						<td class="form1bak" valign="top" colspan="2" >
							<html:text styleClass="formButton"  maxlength="5" property="hoursPerDay" onkeypress="return soloNumeros(event,this);" />
           				</td>
					</tr>

					<tr> 
						<td class="form1bak" valign="middle">
							<font class="formLabelClass"><bean:message key="prompt.employee.office.location"/> <b>*</b> </font>
						</td>
						<td class="form1bak" valign="top" colspan="2" >
							<vectrics:strictLocationCombo property="location" locationType="job.location" selected="<%=locationCode %>" />
                        </td>
					</tr>

					<tr> 
						<td class="form1bak" valign="middle">
							<font class="formLabelClass"><bean:message key="prompt.employee.mentor"/>  </font>
						</td>
						<td class="form1bak" valign="top" colspan="2">
							<vectrics:personPicker 
								remoteMethod="get.matching.employee"
								contextPath="/hrs-intranet"
								caption="Mentor"
								paramName="idMentor"
								popupLeft="200"
								popupTop="90"
								styleClase="formStaticClass"/>
           				</td>
					</tr>

                    <tr>
                       <td>
                           <font class="form1bak"><bean:message key="prompt.employee.elegibleMentor"/></font>
                       </td>
                       <td>
                       		<html:checkbox property="elegibleMentor" />
                       </td>
                    </tr>
					
					<tr>
						 <td>
                           <font class="form1bak"><bean:message key="prompt.employee.elegibleProjectMember"/></font>
                       </td>
                       <td>
                       		<html:checkbox property="elegibleProjectMember" />
                       </td>
					</tr>
					
                    <tr>
                       <td>
                           <font class="formLabelClass"><bean:message key="prompt.employee.medical.Insurance"/></font>
                       </td>
                       <td>
                      		<vectrics:MedicalInsuranceCombo property="medicalInsurance" value="<%=medicalInsuranceCode%>"/>
                       </td>
                    </tr>
                    <tr>
                       	<td class="form1bak" valign="middle" > 					
                           <font class="formLabelClass"><bean:message key="prompt.employee.last.agreement.day"/></font>
                       	</td>
                       	<td>
                       	   		<html:text property="lastAgreementDay" styleId="lastAgreementDay" styleClass="formButton" maxlength="10"/>
								     <input type="button" id="lastAgreementDayBt" name="lastAgreementDayBt" value="..." /> 
											<script type="text/javascript">
	  												Calendar.setup(
	    												{
	      												inputField  : "lastAgreementDay",        // ID of the input field
	      												ifFormat    : "%d/%m/%Y",    			 // the date format
	      												button      : "lastAgreementDayBt"       // ID of the button
	    												}
	  												);
											</script>
                       	</td>
                    </tr>
					<tr>
					<td valign="TOP" align="left"></td>
						<td valign="TOP" align="left">
							<vectrics:formButton elementId="ok.btn" code="button.ok" phraseCode="<%=phraseButton%>" value="submit" name="submit"/>
							&nbsp;
							<vectrics:formButton elementId="cancel.btn" code="button.cancel" phraseCode="button.cancel" value="cancel" name="cancel"/>						
						</td>
					</tr>

				</table>
			</td>
			
		</tr>
	</table>

</html:form>

<hexacta:employmentConditionHistory 
        companyId="<%=idCompany%>" 
        personId="<%=idPerson%>"
        viewOptions="<%=viewOption%>"
 />

<script type="text/javascript">
	reviewCondition();
</script>

